# koroFileHeader at Yungoal acer
# Create: 2021-08-13 13:37:29
# LastEdit: 2021-08-13 16:12:38
"""说明暂无"""
__author__ = '749B'


import sys
import unittest
from unittest.mock import patch, call

if __name__ == '__main__':
    import os
    sys.path.append(os.getcwd())
# pylint: disable = import-error
from azure_client.models import RuncmdAsyncJob, RuncmdTask


class PrintLineTestCase(unittest.TestCase):

    def setUp(self):
        patcher_job_print_func = patch('azure_client.models.RuncmdAsyncJob.print_func')
        patcher_task_print_title = patch('azure_client.models.RuncmdTask.print_title')
        patcher_task_print_row = patch('azure_client.models.RuncmdTask.print_row')
        self.addCleanup(patcher_job_print_func.stop)
        self.addCleanup(patcher_task_print_title.stop)
        self.addCleanup(patcher_task_print_row.stop)
        self.mock_method_job_print_func = patcher_job_print_func.start()
        self.mock_method_task_print_title = patcher_task_print_title.start()
        self.mock_method_task_print_row = patcher_task_print_row.start()

        self.job = RuncmdAsyncJob(None)

        self.data = {
            'vm_name': 'host1',
            'resourceGroup': 'rgroup2',
            'OSType': 'LinuxForTest',
            'SubName': 'Sub4',
        }
        self.check_items = ['这个不重要']
        self.task = RuncmdTask(data=self.data, check_items=self.check_items)

    def test_print_line_with_title(self):
        self.mock_method_task_print_title.return_value = ['vm_info', 'check_items', 'ERR_code', 'ERR_message' ]
        self.mock_method_task_print_row.return_value = ['info1', 'items1', '', '' ]
        self.job.print_line(self.task)
        
        self.mock_method_task_print_title.assert_called_once_with()
        self.mock_method_task_print_row.assert_called_once_with(self.job.rm_tag)
        
        calls = [
            call(*['vm_info', 'check_items', 'ERR_code', 'ERR_message' ], None), 
            call(*['info1', 'items1', '', '' ], None)
        ]
        self.mock_method_job_print_func.assert_has_calls(calls)

    def test_print_line_without_title(self):
        self.mock_method_task_print_title.return_value = ['vm_info', 'check_items', 'ERR_code', 'ERR_message' ]
        self.mock_method_task_print_row.return_value = ['info1', 'items1', '', '' ]
        self.job.show_count_num = 1
        self.job.print_line(self.task)
        self.mock_method_task_print_row.assert_called_once_with(self.job.rm_tag)
        self.mock_method_job_print_func.assert_called_once_with(*['info1', 'items1', '', '' ], None)

    def test_print_multline_with_title(self):
        self.job.multline = True
        self.mock_method_task_print_title.return_value = ('vm_info1,vm_info2', 'username,shell', 'ERR_code', 'ERR_message')
        self.mock_method_task_print_row.return_value = (
            'info1,info2',
            ['root,/bin/bash', 'azureuser,/bin/bash', 'nginx,/sbin/nologin'],
            '', 
            ''
        )
        self.job.print_line(self.task)

        self.mock_method_task_print_title.assert_called_once_with()
        self.mock_method_task_print_row.assert_called_once_with(self.job.rm_tag, multline=True)
        
        calls = [
            call(*['vm_info1,vm_info2', 'username,shell', 'ERR_code', 'ERR_message' ], None), 
            call('info1,info2', 'root,/bin/bash', '', '', None),
            call('info1,info2', 'azureuser,/bin/bash', '', '', None),
            call('info1,info2', 'nginx,/sbin/nologin', '', '', None)
        ]
        self.mock_method_job_print_func.assert_has_calls(calls)

    def test_print_multline_without_title(self):
        self.job.multline = True
        self.job.show_count_num = 1
        self.mock_method_task_print_title.return_value = ('vm_info1,vm_info2', 'username,shell', 'ERR_code', 'ERR_message')
        self.mock_method_task_print_row.return_value = (
            'info1,info2',
            ['root,/bin/bash', 'azureuser,/bin/bash', 'nginx,/sbin/nologin'],
            '', 
            ''
        )
        self.job.print_line(self.task)

        self.mock_method_task_print_row.assert_called_once_with(self.job.rm_tag, multline=True)
        
        calls = [
            call('info1,info2', 'root,/bin/bash', '', '', None),
            call('info1,info2', 'azureuser,/bin/bash', '', '', None),
            call('info1,info2', 'nginx,/sbin/nologin', '', '', None)
        ]
        self.mock_method_job_print_func.assert_has_calls(calls)

    def test_print_multline_with_error(self):
        self.job.multline = True
        self.mock_method_task_print_title.return_value = ('vm_info1,vm_info2', 'username,shell', 'ERR_code', 'ERR_message')
        self.mock_method_task_print_row.return_value = (
            'info1,info2',
            ['root,/bin/bash', 'azureuser,/bin/bash', 'nginx,/sbin/nologin'],
            'error001', 
            'Test Error'
        )
        self.job.print_line(self.task)

        self.mock_method_task_print_title.assert_called_once_with()
        self.mock_method_task_print_row.assert_called_once_with(self.job.rm_tag, multline=True)
        
        calls = [
            call(*['vm_info1,vm_info2', 'username,shell', 'ERR_code', 'ERR_message' ], None), 
            call('info1,info2', 'root,/bin/bash', 'error001', 'Test Error', None),
            call('info1,info2', 'azureuser,/bin/bash', 'error001', 'Test Error', None),
            call('info1,info2', 'nginx,/sbin/nologin', 'error001', 'Test Error', None)
        ]
        self.mock_method_job_print_func.assert_has_calls(calls)

    def test_print_multline_without_stdout(self):
        self.job.multline = True
        self.job.show_count_num = 1

        self.mock_method_task_print_title.return_value = ('vm_info1,vm_info2', 'username,shell', 'ERR_code', 'ERR_message')
        self.mock_method_task_print_row.return_value = (
            'info1,info2',
            '',
            'error001', 
            'Test Error'
        )
        self.job.print_line(self.task)

        calls = [
            call(self.job.rm_tag, multline=True),  # 先多行模式调用一次，但是没有stdout
            call(self.job.rm_tag),                 # 再调用一次普通模式，可以让输出的列对齐
        ]
        self.mock_method_task_print_row.assert_has_calls(calls)

        self.mock_method_job_print_func.assert_called_once_with('info1,info2', '', 'error001', 'Test Error', None)


if __name__ == '__main__':
    unittest.main()

